#include <stdio.h>

int fact(int n){
    int sum = 0;
    if(n==2)
        return n;
    else{
        while (n!=0)
        {
            sum += n % 10;
            n /= 10;
        }
        return fact(sum);
    }
}

int main(int argc, char *argv[]) {

    int num;
    while (scanf("%d",&num)!=EOF)
    {
        if(num==0)
            break;
        num=fact(num);
        printf("%d\n", num);
    }
    return 0;
}